<template>
  <div class="container-classify">
    <el-form ref="formRef" :model="params" inline label-width="80px">
      <el-form-item prop="name" label="分类">
        <el-input v-model="params.name" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="timer" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" type="datetimerange" range-separator="-"
          start-placeholder="开始时间" end-placeholder="结束时间" @change="timerChange">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList()">查询</el-button>
        <el-button type="warning" @click="resetPamars()">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="add">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" v-loading="loading" style="width: 100%">
      <el-table-column label="序号" align="center" width="80">
        <template slot-scope="{$index}">
          <div>
            {{ $index + (params.page - 1) * params.limit + 1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="分类名称" align="center" prop="name"></el-table-column>
      <el-table-column label="分类排序" align="center" prop="sort" width="80"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="create_time"></el-table-column>
      <el-table-column label="操作" align="center" width="260">
        <template slot-scope="{row}">
          <div>
            <el-button type="text" @click="eidt(row.id)">编辑</el-button>
            <el-button type="text" @click="del(row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="params.page" :page-sizes="[10, 20, 50, 100]"
      :page-size="params.limit" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>

    <el-dialog title="新增分类" center :close-on-click-modal="false" :visible.sync="classifyDialogVisible" width="30%" :before-close="handleClose">
      <el-form ref="classifyFormRef" :model="classifyForm" :rules="classifyFormRules" label-width="80px">
        <el-form-item prop="name" label="分类">
          <el-input v-model="classifyForm.name" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item prop="sort" label="序号">
          <el-input-number v-model="classifyForm.sort" :min="0" :step="1" :precision="0"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submintHandle">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import seviceApi from '@/api/serviceApi.js'
export default {
  data () {
    return {
      params: {
        page: 1,
        limit: 10,
        name: '',
        start_time: null,
        end_time: null
      },
      timer: null,
      tableData: [],
      loading: false,
      totalPage: 0,
      classifyDialogVisible: false,
      classifyForm: {
        name: '',
        sort: ''
      },
      classifyFormRules: {
        name: { required: true, message: '分类名称不能为空', trigger: 'blur' },
        sort: { required: true, message: '置顶排序不能为空', trigger: 'blur' }
      },
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.loading = true
      seviceApi.getInformationClassifyList(this.params).then(res => {
        if (res && res.data.code == 200) {
          this.tableData = res.data.page.list
          this.totalPage = res.data.page.totalCount
          this.loading = false
        }
      })
    },
    timerChange (e) {
      console.log(e)
      if (e) {
        this.params.start_time = e[0]
        this.params.end_time = e[1]
      } else {
        this.params.start_time = null
        this.params.end_time = null
      }
    },
    // 每页数
    sizeChangeHandle (val) {
      this.params.limit = val
      this.getList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.params.page = val
      this.getList()
    },
    // 查询
    getDataList () {
      this.params.page = 1
      this.getList()
    },
    resetPamars () {
      this.timer = null
      this.params.start_time = null
      this.params.end_time = null
      this.$refs.formRef.resetFields()
      this.getDataList()
    },
    add () {
      this.classifyDialogVisible = true
    },
    eidt (id) {
      seviceApi.getInformationClassifyInfo(id).then(res => {
        if (res && res.data.code == 200) {
          this.classifyDialogVisible = true
          this.$nextTick(() => {
            this.classifyForm = res.data.data
          })
        }
      })
    },
    del (id) {
      this.$confirm('是否删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        seviceApi.deleteInformationClassify(id).then(res => {
          if (res && res.data.code == 200) {
            this.$message.success('操作成功')
            this.getList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleClose () {
      this.classifyDialogVisible = false
      this.classifyForm.id = ''
      this.$refs.classifyFormRef.resetFields()
    },
    submintHandle () {
      this.$refs.classifyFormRef.validate((valid) => {
        if (valid) {
          let apiUrl = 'informationClassifyAdd'
          if (this.classifyForm.id) {
            apiUrl = 'informationClassifyEdit'
          }
          seviceApi[apiUrl](this.classifyForm).then(res => {
            if (res && res.data.code == 200) {
              this.$message.success(this.classifyForm.id ? '编辑成功' : '添加成功')
              this.handleClose()
              this.getList()
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style lang="scss" scoped></style>